layui.use(['layer', 'miniTab' , 'echarts'], function () {
    var $ = layui.jquery,
        layer = layui.layer,
        miniTab = layui.miniTab,
        echarts = layui.echarts;
        miniTab.listen();
        var token = sessionStorage.getItem('newtoken');
         /*********定时器********/
        //  setInterval(function(){
        //      console.log(5);
             
        //     offLineRate();
        //     AbnorBatteryRate();
        //     AbnorBatteryData();                     
        //  },100000)
         /**********************/

        /***********终端离线率**************/
        function offLineRate(){
                    $.ajax({          
                        url: '/oms-web/statistics/offLineRate?token='+ token,
                        dataType:"json",  
                        contentType:"application/json",
                        success:function(data){
                            if (data.code == -1) { window.location='../page/login-1.html';     }
                        if(data.code === 0){
                              // 数字缓动
                            new CountUp('offline',data.data.out, 0, 2.5, options).start();
                            new CountUp('online', (data.data.sum - data.data.out), 0, 2.5, options).start();
                            var out = data.data.out;
                            var on = data.data.sum - data.data.out;
                            var rate = data.data.rate;
                                rate = Number(rate) * 100;
                                num = Math.floor(rate * 100)  / 100;
                                var x = 2;
                                var k = '在线';
                                var u = '离线';
                            getRound(out,on,num,x,u,k)
                    }
                },
                        error:function () {
                            // layer.msg("数据请求错误,请检查时间格式！");
                        }
                });
        }
        offLineRate()       


        /***********电池异常环形图**************/
        function AbnorBatteryRate(){
                    $.ajax({          
                        url: '/oms-web/statistics/AbnorBatteryRate?token='+ token,
                        dataType:"json",  
                        contentType:"application/json",
                        success:function(data){
                            if (data.code == -1) { window.location='../page/login-1.html';     }
                        if(data.code === 0){
                            new CountUp('normalCell',(data.data.sum - data.data.out), 0, 2.5, options).start();
                            new CountUp('abnormalCell', data.data.out, 0, 2.5, options).start();
                            var out = data.data.out;
                            var on = data.data.sum - data.data.out;
                            var rate = data.data.rate;
                                rate = Number(rate) * 100;
                                num = Math.floor(rate * 100)  / 100;
                                var x = 0;
                                var u = '异常';
                                var k = '正常';
                            getRound(out,on,num,x,u,k);
                    }
                },
                        error:function () {
                            // layer.msg("数据请求错误,请检查时间格式！");
                        }
                });
        }
        AbnorBatteryRate()       
        
        /***********异常数据形图**************/
        function AbnorBatteryData(){
                    $.ajax({          
                        url: '/oms-web/statistics/AbnorDataRate?token='+ token,
                        dataType:"json",  
                        contentType:"application/json",
                        success:function(data){
                            if (data.code == -1) { window.location='../page/login-1.html';     }
                        if(data.code === 0){
                            new CountUp('normalData',(data.data.sum - data.data.out), 0, 2.5, options).start();
                            new CountUp('abnormalData', data.data.out, 0, 2.5, options).start();
                            var out = data.data.out;
                            var on = data.data.sum - data.data.out;
                            var rate = data.data.rate;
                                rate = Number(rate) * 100;
                                num = Math.floor(rate * 100)  / 100;
                            var x = 1;
                            var u = '异常';
                            var k = '正常';
                            getRound(out,on,num,x,u,k)
                    }
                },
                        error:function () {
                            // layer.msg("数据请求错误,请检查时间格式！");
                        }
                });
        }
        AbnorBatteryData()       
        
    /**
     * 圆环表
     */
     function getRound(out,on,rate,x,u,k){
         if(x == 0){
            var echartsPies = echarts.init(document.getElementById('echarts-pies'), 'walden');
         }else if(x == 1){
            var echartsPies = echarts.init(document.getElementById('echarts-pies1'), 'walden');
         }else if(x == 2){
            var echartsPies = echarts.init(document.getElementById('echarts-pies2'), 'walden');
         }

        option = {
                title:{
                        show:true,
                        text:rate +'%' +'\n'+ u + '率',
                        x:'center',
                        y:'center',
                        textStyle: {
                            fontSize: '13',
                            color:'#666',
                            fontWeight: 'normal',
                        }
                   },
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}: {c} ({d}%)'
                    // formatter: "{d}%",
                    // show:true
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    show:false
                },
                color:['green','red'],
                series: 
                    {
                        name:'',
                        type:'pie',
                        radius: ['65%', '85%'],
                        avoidLabelOverlap: true,
                        hoverAnimation:false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center',
                            },
                            emphasis: {
                                show: false
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data:[
                            {value:on, name:k},
                            {value:out, name:u}
                        ]
                    }
                
            };
            echartsPies.setOption(option);

                // 重绘
            window.onresize = function () {
                echarts.init(document.getElementById("echarts-pies")).resize();
                echarts.init(document.getElementById("echarts-pies1")).resize();
                echarts.init(document.getElementById("echarts-pies2")).resize();
            };
           
     }
});